package com.e3.template.tgf;

import java.util.ArrayList;
import java.util.List;

import com.e3.template.model.TGFTreeModel;
import com.e3.web.core.client.GridListener;
import com.e3.web.core.client.QueryListener;
import com.e3.web.core.client.TreeListener;
import com.e3.web.core.model.SuperQC;
import com.e3.web.widget.client.BaseAction;
import com.e3.web.widget.client.MyButton;
import com.e3.web.widget.client.MyMenu;
import com.e3.web.widget.client.MyRefDialog;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.menu.Menu;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.user.client.Element;

public abstract class TGFRefDialog<D extends TGFTreeModel> extends MyRefDialog<D> implements TreeListener<D>,QueryListener<D>,GridListener<D> {
	
	private SelectionListener<ButtonEvent> btnSelectListener = new SelectionListener<ButtonEvent>() {
		@Override
		public void componentSelected(ButtonEvent ce) {
			MyButton item = (MyButton)ce.getSource();
			onBtnMnuEvent(item.getAction());
		}
	};
	
	private D selectedModel;
	
	private ContentPanel contentPanel = new ContentPanel(new BorderLayout());
	
	private ToolBar toolbar = new ToolBar();
	
	private MyButton queryButton = new MyButton(TGFStateMachine.QUERY_ACTION);
	
	private TGFGridPanel<D> gridPanel;
	
	private TGFTreePanelAsync<D> treePanel;
	
	private TGFQueryDialog<D> queryDialog;
	
	@Override
	protected void onRender(Element parent, int pos) {
		
		super.onRender(parent, pos);
		
		this.setSize(600, 450);
		this.setHeading(getRefDialogTitle());
		setLayout(new FitLayout());
			
		BorderLayoutData bld = new BorderLayoutData(LayoutRegion.WEST);
		bld.setSplit(true);
		bld.setMargins(new Margins(0,2,0,0));
		contentPanel.add(getTreePanel(),bld);
		
		bld = new BorderLayoutData(LayoutRegion.CENTER);
		contentPanel.add(getGridPanel(),bld);
		
		initToolBar();
		
		contentPanel.setHeaderVisible(false);
		this.add(contentPanel);
	}
	
	@Override
	public void onRefWindowShow() {
		if(selectedModel!=null){
			List<D> models = new ArrayList<D>(1);
			models.add(selectedModel);
			getGridPanel().setGridValues(models);
		}
	}

	@Override
	public D getRefValue() {
		if(!isVisible()){
			return selectedModel;
		}
		List<D> models = getGridPanel().getSelectedModels();
		if(models!=null&&models.size()>0){
			return models.get(0);
		}
		return null;
	}

	@Override
	public void setRefValue(D value) {
		selectedModel = value;
	}
	
	private void onBtnMnuEvent(BaseAction action) {
		if(action == TGFStateMachine.QUERY_ACTION){
			onQuery();
		}
	}
	
	private void onQuery(){
		getQueryDialog().setVisible(true);
	}

	private void initToolBar(){
		queryButton.addSelectionListener(btnSelectListener);
		toolbar.add(queryButton);
		contentPanel.setTopComponent(toolbar);
	}
	
	public TGFGridPanel<D> getGridPanel(){
		if(gridPanel == null){
			gridPanel = createGridPanel();
			gridPanel.addGridListener(this);
		}
		return gridPanel;
	}
	
	public TGFTreePanelAsync<D> getTreePanel(){
		if(treePanel == null){
			treePanel = createTreePanel();
			treePanel.addTreeListener(this);
			treePanel.setTreeContextMenu(getTreeContextMenu(treePanel));
			treePanel.setMaskComponent(this);		
		}
		return treePanel;
	}
	
	protected TGFQueryDialog<D> getQueryDialog() {
		if(queryDialog == null){
			queryDialog = createQueryDialog();
			queryDialog.setLocalName(getCurrentLocalName());
			queryDialog.addQueryListener(this);
		}
		return queryDialog;
	}
	
	protected Menu getTreeContextMenu(TGFTreePanelAsync<D> treePanel){
		MyMenu menu = new MyMenu();
		return menu;
	}
	
	public void onTreeDataSelected(final D model,List<D> submodels) {
		getGridPanel().setGridValues(submodels);
	}
	
	public void onQuerySuccessed(SuperQC qc,List<D> models) {
		getGridPanel().setGridValues(models);
	}

	public void onGridSelectionChanged(D model) {
		selectedModel = model;
	}

	public void onGridRowDoubClicked(D model) {
		onOk();
	}
	
	public List<D> getSelectedModels(){
		return getGridPanel().getSelectedModels();
	}

	protected abstract String getRefDialogTitle();
	
	protected abstract TGFGridPanel<D> createGridPanel();
	
	protected abstract TGFTreePanelAsync<D> createTreePanel();
	
	protected abstract TGFQueryDialog<D> createQueryDialog();
	
	protected abstract String getCurrentLocalName();

}
